package org.tooyoung.fourone.user.activity;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import org.json.JSONArray;
import org.tooyoung.fourone.R;
import org.tooyoung.fourone.user.model.UserImformation;
import java.util.ArrayList;
import java.util.Arrays;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {
    public final static String[] PRIVINCE={"北京市","浙江省","安徽省","上海市","福建省","重庆市","江西省","山东省","河南省","内蒙古自治区",
            "湖北省","新疆维吾尔自治区","湖南省","宁夏回族自治区","广东省","西藏自治区","海南省广西壮族自治区",
            "四川省","河北省","贵州省","山西省","云南省","辽宁省","陕西省","吉林省",
            "甘肃省","黑龙江省","青海省", "江苏省","澳门特别行政区","台湾省香港特别行政区"};
    private TextView mTxt_username;
    private TextView mTxt_userpassword;
    private Spinner mTxt_privice;
    private TextView mTxt_detailaddress;
    private TextView mTxt_phonenumber;
    private TextView mTxtemail;
    private boolean flag;
    private Button mButton;
    private TextView mTxt_alerm;
    private Spinner mSpinner_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Bmob.initialize(this, "1c20f8eef3a087e65f6e1012ac4307f4");
        mButton = (Button) findViewById(R.id.regist_btn_commit);
        mTxt_username = (TextView) findViewById(R.id.text_username);
        mTxt_userpassword = (TextView) findViewById(R.id.text_userpassword);
        mTxt_privice = (Spinner) findViewById(R.id.text_privice);
        mTxt_detailaddress = (TextView) findViewById(R.id.text_detailaddress);
        mTxt_phonenumber = (TextView) findViewById(R.id.text_phonenumber);
        mTxtemail = (TextView) findViewById(R.id.text_email_address);
        mSpinner_sex = (Spinner) findViewById(R.id.spinner_sex);
        mButton.setOnClickListener(this);
        mTxt_username.setOnFocusChangeListener(this);
        ArrayList<String> list = new ArrayList<>();
        list.add("男");
        list.add("女");
        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
        mSpinner_sex.setAdapter(adapter);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, Arrays.asList(PRIVINCE));
        mTxt_privice.setAdapter(adapter1);

    }

    @Override
    public void onClick(View v) {
        String usersex = mSpinner_sex.getSelectedItem().toString().trim();
        final String username = mTxt_username.getText().toString().trim();
        final String userpassword = mTxt_userpassword.getText().toString().trim();
        String userprivice = mTxt_privice.getSelectedItem().toString().trim();
        String userdetailAddress = mTxt_detailaddress.getText().toString().trim();
        String userPhoneNumber = mTxt_phonenumber.getText().toString().trim();
        String userEmail = mTxtemail.getText().toString().trim();
        if (username != null && userpassword != null && userprivice != null && userPhoneNumber != null && userEmail != null && userdetailAddress != null&&usersex!=null) {

            new Thread() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().createAccount(username, userpassword);
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            }.start();

            UserImformation userImformation = new UserImformation();
            userImformation.setUsername(username);
            userImformation.setSex(usersex);
            userImformation.setUserpassword(userpassword);
            userImformation.setPrivince(userprivice);
            userImformation.setAddressDetail(userdetailAddress);
            userImformation.setPhoneNumber(userPhoneNumber);
            userImformation.setEmailAddress(userEmail);
            userImformation.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e != null) {
                        Toast.makeText(RegisterActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(RegisterActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            Toast.makeText(this, "必填信息不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d("Register", "onFocusChange:" + hasFocus);
        if (hasFocus) {
            flag = true;
        } else {
            if (flag) {
                BmobQuery query = new BmobQuery("UserImformation");
                query.addWhereEqualTo("username", mTxt_username.getText().toString().trim());
                query.findObjectsByTable(new QueryListener<JSONArray>() {
                    @Override
                    public void done(JSONArray jsonArray, BmobException e) {
                        if (jsonArray.length() != 0) {
                            mButton.setEnabled(false);
                            Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                        } else {
                            mButton.setEnabled(true);
                            Toast.makeText(RegisterActivity.this, "可以注册", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    }
}

